package controller

import (
	"editor/constant"
	"editor/utils"
	"github.com/gin-gonic/gin"
	"net/http"
)

func LoginHandler(c *gin.Context) {
	// 已登录判断
	if utils.GetSession(c, constant.UserName) == constant.Config.Login.User {
		c.HTML(http.StatusOK, "redirect.html", gin.H{
			"Title": constant.Config.App.Name,
			"Msg":   "已登录，请勿重复登陆",
			"Time":  1,
			"Url":   "/",
		})
		return
	}

	c.HTML(http.StatusOK, "login.html", gin.H{
		"Title": constant.Config.App.Name,
	})
}
func DoLoginHandler(c *gin.Context) {
	// 已登录判断
	if utils.GetSession(c, constant.UserName) == constant.Config.Login.User {
		c.HTML(http.StatusOK, "redirect.html", gin.H{
			"Title": constant.Config.App.Name,
			"Msg":   "已登录，请勿重复登陆",
			"Time":  1,
			"Url":   "/",
		})
		return
	}

	if c.PostForm("userName") == constant.Config.Login.User &&
		c.PostForm("userPass") == constant.Config.Login.Pass {
		utils.SetSession(c, constant.UserName, constant.Config.Login.User, 86400*7)
		c.HTML(http.StatusOK, "redirect.html", gin.H{
			"Title": constant.Config.App.Name,
			"Msg":   "登录成功",
			"Time":  1,
			"Url":   "/admin",
		})
	} else {
		c.HTML(http.StatusUnauthorized, "redirect.html", gin.H{
			"Title": constant.Config.App.Name,
			"Msg":   "用户名或密码错误",
			"Time":  3,
			"Url":   "/login",
		})
	}
	return
}
